package ls.vuecp.gui.toolbar;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;


public class VueToolPanel extends JPanel
{
	// when we can set this to false, we can get rid of this class.
    public static final boolean IS_CONTEXTUAL_TOOLBAR_ENABLED = false;
       
    //private JComponent mMainToolBar = null;	/** the panel where the main tools are placed **/	   
    private ButtonGroup mButtonGroup = null;	/** the button group used for tool selection **/   
    private Vector mTools = new Vector();		/** the list of VueTools in the main tool panel **/  
    private VueTool mCurrentTool = null;		/** the current tool selection (TO DO:  remove this)  **/
    //private Box mMainBox = null;	  
    private Map mToolButtons = new HashMap();     /** a map of PaletteButtons keyed off of the tool ID **/
    private static final boolean debug = false;
    
    public VueToolPanel() {
        mButtonGroup = new ButtonGroup();
        if (debug)
            setBackground(Color.blue);
//        else
//            GUI.applyToolbarColor(this);
		
        setLayout( new BoxLayout(this,BoxLayout.X_AXIS) );
        setOpaque(false);
//        if (GUI.isMacBrushedMetal())
//            setBorder(new EmptyBorder(0,3,2,10));//tlbr
//        else
            setBorder(new EmptyBorder(0,3,0,10));//tlbr
    }
    

    public JComponent getMainToolbar() 
    {
        return this;
    }
	
    public void addSeparator()
    {
    	JLineSeparator jsp = new JLineSeparator(SwingConstants.VERTICAL,3,18);
    	jsp.setBorder(BorderFactory.createEmptyBorder());
    	add(jsp);
    }
	
	private static final JPanel p= new JPanel();

    
// --- add tools -------------------------------------------------------	
    public void addTools( VueTool [] pTools, int[] separators) {
    	int separatorIndex =0;
        for( int i=0; i<pTools.length + separators.length; i++) {
        	if ((separatorIndex < separators.length) && i==separators[separatorIndex])
        	{
        		addSeparator();
        		separatorIndex++;
        	}
        	else
        		addTool( pTools[i-separatorIndex],true );
        }
    }
	
    public void addTool( VueTool pTool) { addTool(pTool,false);    }
//    public void addButton(JButton button)
//    {
//    	this.add(button);
//    }
    public void addTool( VueTool pTool,boolean addToMainToolbar) 
    {	
        if( mTools == null) mTools = new Vector();
        
        mTools.add(pTool);
        
        //if(pTool.hasToolbarButton()) 
        {
        	PaletteButton button = createPaletteToolButton(pTool);
        	
        	if (addToMainToolbar)	add( button); 
            
            mToolButtons.put( pTool.getID(), button);
            
            addToButtonGroup(button);
            
            addSubTools(pTool);
        }
    }
    private void addSubTools(VueTool pTool)
    {
    	// save teh component in the button map
        if (pTool.hasSubTools())
        {
        	Vector v = pTool.getSubToolIDs();
        	for (int i = 0; i < v.size(); i++)
        	{
        		VueTool tool = pTool.getSubTool((String)v.get(i));
        		addTool(tool,false);
        	}
        }
    }

    private PaletteButton createPaletteToolButton(VueTool pTool)
    {
    	PaletteButton button = createPaletteButton(pTool);
    	
    	// set the user context to the VueTOol
        button.setContext( pTool);
        button.addActionListener( pTool);
        // store the button in the tool
        pTool.setLinkedButton(button);
               
        // todo: setting this mnemonic doesn't appear to work
        //if (pTool.getShortcutKey() != 0)
        //button.setMnemonic(pTool.getShortcutKey());
        
        return button;
    }
    protected PaletteButton createPaletteButton(VueTool pTool)
    {
        PaletteButton button = null;
        
        if (pTool.hasSubTools()) 
        {
        	PaletteButtonItem items [] = createPaletteButtonItems(pTool);
            
        	button = new PaletteButton( items );
            button.setPropertiesFromItem( items[0]);
            button.setOverlayIcons (pTool.getOverlayUpIcon(), pTool.getOverlayDownIcon() );
        } 
        else  
        {  // just a radio-like button, no popup items 
            button = new PaletteButton();
            button.setIcons( pTool.getIcon(), pTool.getDownIcon(), pTool.getSelectedIcon() ,
                             pTool.getDisabledIcon(), pTool.getRolloverIcon() );
        }
        
        button.setToolTipText( pTool.getToolTipText() );
        button.setName(pTool.getID());
        
        setToolButtonBackground(button);
        
        return button;
    }
    private PaletteButtonItem [] createPaletteButtonItems(VueTool pTool)
    {
    	// create button items
        Vector names = pTool.getSubToolIDs();
        int numSubTools = names.size();
        
        String name;
        VueTool subTool;
        PaletteButtonItem item;
        PaletteButtonItem items [];
        
        items = new PaletteButtonItem[numSubTools];
        for(int i=0; i<numSubTools; i++) 
        {
            if( (subTool = pTool.getSubTool( (String)names.get(i))) != null) 
            {
            	item = createPaletteButtonItem(subTool);			
                items[i] = item;
                subTool.setLinkedButton(item);
            }
        }
        return items;
    }
    private PaletteButtonItem createPaletteButtonItem(VueTool tool)
    {
    	PaletteButtonItem item = new PaletteButtonItem();
    	
        item.setName(tool.getID());//name);
        item.setIcon( tool.getIcon() );
        item.setSelectedIcon( tool.getSelectedIcon() );
        item.setDisabledIcon( tool.getDisabledIcon() );
        item.setRolloverIcon( tool.getRolloverIcon() );
        item.setPressedIcon( tool.getDownIcon() );
        item.setMenuItemIcon( tool.getMenuItemIcon() );
        item.setMenuItemSelectedIcon( tool.getMenuItemSelectedIcon() );
        item.setToolTipText( tool.getToolTipText() );
        //item.setToolTipText( pTool.getToolTipText() );
        
        item.addActionListener( tool);
        
        return item;
    }
	
    private void setToolButtonBackground(PaletteButton pButton)
    {
    	if (debug)
            pButton.setBackground(Color.magenta);
//        else if (!VUE.isApplet())
//            GUI.applyToolbarColor(pButton);
        else
        {
        	//For the life of me I can't figure out what's wrong with L&F on applets but 
        	//buttons are not getting the same background properties as panels, weird. Hack
        	//for now.
        	pButton.setBackground(p.getBackground());
        }
        
    }
	private void addToButtonGroup( PaletteButton pButton) 
	{
        mButtonGroup.add( pButton);       
        if( mButtonGroup.getButtonCount() == 1) {   pButton.setSelected( true);  }
    }
	
    /**
     * getSelectedTool
     * This method returns the selected tool based on the radio group
     **/
    public VueTool getSelectedTool() {
	 	
        Enumeration e = mButtonGroup.getElements();
        PaletteButton cur;
        while( e.hasMoreElements() ) {
            cur = (PaletteButton) e.nextElement();
            if( cur.isSelected() ) {
                return  ((VueTool) cur.getContext()) ;
            }
        }
        return null;	 	
    }
	
	
    /**
     * setSelectedTool
     *This method attempts to set the currenlty selected tool
     * in the main tool bar by looking for the TVueTool's PaletteButton
     * that's in the radio group.  If found, it selectes the button
     * in the radio group and causes an repaint.  
     *
     * @param VueTool - the new tool to select
     **/
    public void setSelectedTool( VueTool pTool) {
        if( pTool != null) {      
        	if (pTool.getParentTool() != null)
        	{        		
        		//you've selected a subtool...do somethign else.
        		//select the parent.
        		VueTool parentTool = pTool.getParentTool();
        		PaletteButton button = (PaletteButton) mToolButtons.get( parentTool.getID() );
        		
        		
        		//PaletteButtonItem[] items = button.getPaletteButtonItems();
        		button.setPropertiesFromItem((PaletteButton) mToolButtons.get(pTool.getID()));
        		
        		parentTool.setSelectedSubTool(pTool);
        		//button.setContext( pTool);
        		if( button != null) 
        			mButtonGroup.setSelected( button.getModel(), true);
        		
        		//button.addActionListener( pTool);
        		//button.setSelectedIcon(pTool.getSelectedIcon());        		
        		//button.setIcon(pTool.getIcon());        		
        	}
        	else
        	{        		
        		PaletteButton button = (PaletteButton) mToolButtons.get( pTool.getID() );
        		if( button != null) 
        			mButtonGroup.setSelected( button.getModel(), true);
            }
        }
    }
		
    private JPanel mPanelContent;

    public void removeTool( VueTool pTool) {
		
        PaletteButton button = (PaletteButton) mToolButtons.get( pTool.getID() );
        mToolButtons.remove( pTool.getID() );
		
        mTools.remove( pTool);
        // FFIX:  tbd we might not need to ever remove, only disable.
        // removeToolButton( pTool.getName() );
    }
	
	public Map getToolButtons()
	{
		return  mToolButtons;
	}
	
    
	
    protected void X_processMouseMontionEvent( MouseEvent pEvent) {
        //debug("  VueToolPanel: processMouseMotionEvent "+pEvent.getID() );
        super.processMouseEvent( pEvent);
    }


    protected void X_processMouseEvent( MouseEvent pEvent) {
        //debug("  processMouseEvent() "+ pEvent.getID()  );
        super.processMouseEvent( pEvent);
    }

}
